#include "stdafx.h"
#include "net/NameValueCollection.hpp"
#include "stdlib/exception.hpp"
using JHCPP::stdlib::jhException::NotFoundException;
#include <algorithm>

NET_NAMESPACE_BEGIN

CNameValueCollection::CNameValueCollection()
{
}

CNameValueCollection::CNameValueCollection(const CNameValueCollection& nvc) : m_map(nvc.m_map)
{
}

CNameValueCollection::~CNameValueCollection()
{
}

CNameValueCollection& CNameValueCollection::operator = (const CNameValueCollection& nvc)
{
	if (&nvc != this)
	{
		m_map = nvc.m_map;
	}
	return *this;
}

void CNameValueCollection::swap(CNameValueCollection& nvc)
{
	std::swap(m_map, nvc.m_map);
}

const std::string& CNameValueCollection::operator [] (const std::string& name) const
{
	ConstIterator it = m_map.find(name);
	if (it != m_map.end())
		return it->second;
	else
		throw NotFoundException(name);
}

void CNameValueCollection::set(const std::string& name, const std::string& value)	
{
	Iterator it = m_map.find(name);
	if (it != m_map.end())
		it->second = value;
	else
		m_map.insert(HeaderMap::ValueType(name, value));
}

void CNameValueCollection::add(const std::string& name, const std::string& value)
{
	m_map.insert(HeaderMap::ValueType(name, value));
}

const std::string& CNameValueCollection::get(const std::string& name) const
{
	ConstIterator it = m_map.find(name);
	if (it != m_map.end())
		return it->second;
	else
		throw NotFoundException(name);
}

const std::string& CNameValueCollection::get(const std::string& name, const std::string& defaultValue) const
{
	ConstIterator it = m_map.find(name);
	if (it != m_map.end())
		return it->second;
	else
		return defaultValue;
}

bool CNameValueCollection::has(const std::string& name) const
{
	return m_map.find(name) != m_map.end();
}

CNameValueCollection::ConstIterator CNameValueCollection::find(const std::string& name) const
{
	return m_map.find(name);
}

CNameValueCollection::ConstIterator CNameValueCollection::begin() const
{
	return m_map.begin();
}

CNameValueCollection::ConstIterator CNameValueCollection::end() const
{
	return m_map.end();
}

bool CNameValueCollection::empty() const
{
	return m_map.empty();
}

std::size_t CNameValueCollection::size() const
{
	return m_map.size();
}

void CNameValueCollection::erase(const std::string& name)
{
	m_map.erase(name);
}

void CNameValueCollection::clear()
{
	m_map.clear();
}

NET_NAMESPACE_END
